package com.dongpl.jar2mvn.frame;

import com.dongpl.jar2mvn.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

@Component
public class MyFrame extends JFrame {

    @Autowired
    private MyService myService;

    public MyFrame() {
        initUI();
    }

    private void initUI() {
        setTitle("Jar导入Maven");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // ✅ 居中显示
        setLayout(null);
        // ********************************************* Maven环境设置 ****************************************************/
        JTextField mvnPathInput = new JTextField();
        mvnPathInput.setBounds(20, 30, 300, 30);
        mvnPathInput.setEditable(false);  // 禁止编辑
        add(mvnPathInput);
        JButton testMvnButton = new JButton("测试");
        testMvnButton.setBounds(330, 30, 100, 30);
        add(testMvnButton);
        JLabel testMvnOutput = new JLabel("");
        testMvnOutput.setBounds(440, 30, 100, 30);
        add(testMvnOutput);
        // 获取环境变量 PATH
        String path = getMvnPath();
        mvnPathInput.setText(path);
        // 输入框点击事件
        mvnPathInput.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setDialogTitle("请选择Maven路径");
                int result = fileChooser.showOpenDialog(null); // `this` 是 JFrame
                if (result == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    mvnPathInput.setText(file.getAbsolutePath());
                }
            }
        });
        // 测试按钮点击事件
        testMvnButton.addActionListener(e -> {
            String result = myService.testMvn(mvnPathInput.getText());
            testMvnOutput.setText(result);
        });
        // ********************************************* 文件操作 ****************************************************/
        JTextField filePathInput = new JTextField("点击这里选择要操作的jar文件");
        filePathInput.setBounds(20, 80, 300, 30);
        filePathInput.setEditable(false);  // 禁止编辑
        add(filePathInput);
        JButton uploadButton = new JButton("导入");
        uploadButton.setBounds(330, 80, 100, 30);
        add(uploadButton);
        JTextArea resultOutput = new JTextArea("");
//        resultOutput.setBounds(20, 130, 760, 400);
        resultOutput.setEditable(false); // 确保文本区域不可编辑
        JScrollPane scrollPane = new JScrollPane(resultOutput);
        scrollPane.setBounds(20, 130, 760, 400);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); // 根据需要显示水平滚动条
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); // 根据需要显示垂直滚动条
        add(scrollPane);
        // 文件目录点击事件
        filePathInput.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setDialogTitle("请选择文件上传");
                // 设置允许选择文件和目录
                fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                // 添加文件类型过滤器（只对文件有效） 过滤只展示jar文件
                FileNameExtensionFilter filter = new FileNameExtensionFilter("Jar文件", "jar");
                fileChooser.setFileFilter(filter);
                int result = fileChooser.showOpenDialog(null); // `this` 是 JFrame
                if (result == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    filePathInput.setText(file.getAbsolutePath());
                }
            }
        });
        // 导入按钮点击事件
        uploadButton.addActionListener(e -> myService.installJar2Mvn(mvnPathInput.getText(), filePathInput.getText(), resultOutput));
        // 显示窗口
        setVisible(true);
    }

    private void fileUpload() {

    }

    public static String getMvnPath() {
        String os = System.getProperty("os.name").toLowerCase();
        String command = null;
        if (os.contains("win")) { // Windows 系统
            command = "where mvn";
        } else if (os.contains("nix") || os.contains("nux") || os.contains("mac")) { // Linux 或 macOS 系统
            command = "which mvn";
        }
        if (command != null) {
            try {
                Process process = Runtime.getRuntime().exec(command);
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String mvnPath = reader.readLine();
                process.waitFor();
                return mvnPath;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
